Linux 使用Mailx发送邮件

1.开启 QQ 邮箱的 SMTP,获取授权码

1.1 这里使用了 QQ 邮箱的 SMTP,需要做以下配置:

1.2 qq 邮箱通过生成授权码来设置密码 :

2.安装软件包:

1
yum install mail

3.编辑配置文件在/etc/mail.rc :

1
2
3
4
5
6
7
8
9
set from=979915941@qq.com
set smtp=smtp.qq.com
set smtp-auth-user=979915941 #邮箱账号

set smtp-auth-password=ubwrvzzafgdnbbjb #客户端授权码
set smtp-auth=login
set smtp-use-starttls
set ssl-verify=ignore
set nss-config-dir=/root/.certs

4.关闭 sendmail 服务,开启 postfix 服务

1
2
3
4
5
6
7
#sendmial
service sendmail stop
chkconfig sendmail off

#postfix
service postfix start
chkconfig postfix on

5.创建证书文件

1
2
3
4
5
6
7
8
9
10
11
12
13
mkdir -p /root/.certs/    创建文件夹
向文件中输入内容
echo -n | openssl s_client -connect smtp.qq.com:465 | sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p' > ~/.certs/qq.crt

certutil -A -n "GeoTrust SSL CA" -t "C,," -d ~/.certs -i ~/.certs/qq.crt

certutil -A -n "GeoTrust Global CA" -t "C,," -d ~/.certs -i ~/.certs/qq.crt

certutil -L -d /root/.certs

cd /root/.certs 进入.certs 文件夹下

certutil -A -n "GeoTrust SSL CA - G3" -t "Pu,Pu,Pu" -d ./ -i qq.crt

6.验证邮件服务

1
2
echo  hello word | mail -s " title"  979915941@qq.com 
# 979915941@qq.com 会收到一封邮件

7.将日志发送到邮箱中(将所发送的日志作为正文)

1
2
3
4
5
6
7
8
9
mail -s “主题” 收件地址< 文件(邮件正文)

[root@hadoop001 ~]# mail -v -s "test ssl l" 979915941@qq.com </var/log/mail.log

"test ssl l" 表示邮件名字
</var/log/mail.log 表示要发送正文

也可以通过管道符来 发送正文
[root@hadoop001 ~]# cat /var/log/mail.log | mail -s "fujian" 979915941@qq.com

8.查看命令帮助:mail –help

1
2
3
4
[root@hadoop001 ~]# mail --help

mail: illegal option -- -
Usage: mail -eiIUdEFntBDNHRV~ -T FILE -u USER -h hops -r address -s SUBJECT -a FILE -q FILE -f FILE -A ACCOUNT -b USERS -c USERS -S OPTION users

9.查看详细 的命令帮助 man mail

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
-A name
Executes an account command (see below) for name after the startup files have been read.

-a file
Attach the given file to the message.

-B Make standard input and standard output line-buffered.

-b address
Send blind carbon copies to list. List should be a comma-separated list of names.

-c address
Send carbon copies to list of users.

-D Start in disconnected mode; see the description for the disconnected variable option.

-d Enables debugging messages and disables the actual delivery of messages. Unlike -v, this option is
intended for mailx development only.

-e Just check if mail is present in the system mailbox. If yes, return an exit status of zero, else, a non-
zero value.

-E If an outgoing message does not contain any text in its first or only message part, do not send it but
discard it silently, effectively setting the skipemptybody variable at program startup. This is useful
for sending messages from scripts started by cron(8).

-f [file]
Read in the contents of the users mbox (or the specified file) for processing; when mailx is quit, it
writes undeleted messages back to this file. The string file is handled as described for the folder com-
mand below.

-F Save the message to send in a file named after the local part of the first recipient’s address.

-H Print header summaries for all messages and exit.

-h hops
Invoke sendmail with the specified hop count. This option has no effect when SMTP is used for sending
mail.

-i Ignore tty interrupt signals. This is particularly useful when using mailx on noisy phone lines.

-I Shows the ‘Newsgroup:’ or ‘Article-Id:’ fields in the header summary. Only applicable in combination with

10.mail -s “主题” 收件地址 -a附件

1
2
3
4
[root@hadoop001 ~]# cat /var/log/mail.log | mail -s "fujian" -a /var/log/yum.log 979915941@qq.com
[root@hadoop001 ~]#

如果没有给邮件添加 正文,那么回车以后 会让你填写正文,如果不需要正文,直接Ctrl+D,结束

11.编写shell脚本 发送邮件

-a表示附带文件

编辑 up.sh脚本:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
if [ $# -lt 2 ];
then
echo "传入参数有误,请重新传入"
echo "Useage: up.sh <mail-address> <attachment>"
exit 1
fi

first=1

attachment=""

for x in $@
do
if [ $first -eq 1 ];
then
first=0

else
attachment="$attachment -a $x"
fi
done


cat /var/up/uploading.txt | mail -s $1 $attachment user@qq.com

12.运行up.sh脚本:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[root@hadoop001 up]# sh -x up.sh "shell发送QQ邮件" ./haha.jpg ./xixi.png 
+ '[' 3 -lt 2 ']'
+ first=1
+ attachment=
+ for x in '$@'
+ '[' 1 -eq 1 ']'
+ first=0
+ for x in '$@'
+ '[' 0 -eq 1 ']'
+ attachment=' -a ./haha.jpg'
+ for x in '$@'
+ '[' 0 -eq 1 ']'
+ attachment=' -a ./haha.jpg -a ./xixi.png'
+ mail -s $'shell\345\217\221\351\200\201QQ\351\202\256\344\273\266' -a ./haha.jpg -a ./xixi.png user@qq.com
+ cat /var/up/uploading.txt

成功发送邮件:

本文标题:Linux 使用Mailx发送邮件

文章作者:skygzx

发布时间:2019年04月12日 - 11:35

最后更新:2019年04月15日 - 13:02

原始链接:http://yoursite.com/2019/04/12/Linux 使用Mailx发送邮件/

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

-------------本文结束感谢您的阅读-------------
0%